package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.util.YxyResUtil;

import io.swagger.annotations.ApiOperation;

@RestController
public class TopicLessonChapterGetItemController {

    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;
    @Autowired
    private ElasticsearchTemplate et;

    @Autowired
    private YxyResUtil yxyResUtil;

    @ApiOperation(value = "专题课程-微课课程-编辑章节")
    @RequestMapping(value = "/api_yw/manage/tec/lessons/topiclesson/getItem", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        YxyLessonsTopicLessonItem item = yxyLessonsTopicLessonItemRepository.findById(reqModel.itemId);
        Assert.notNull(item, "课时未找到");
        //
        if (item.getVideoId() == null) {
            YxyLessonsVideo video = yxyLessonsVideoRepository.getOne(item.getVideoId());
            model.orderNum = item.getOrderNum() - 20 + 1;
            model.duration = video.getDuration() + "";
            model.preview = yxyResUtil.getPreviewUrl(video.getId());
        }
        //
        String lessonId = item.getLessonId();
        Integer itemOrderNum = item.getOrderNum();

        // 获取上一个 LessThan
        YxyLessonsTopicLessonItem lastItem = yxyLessonsTopicLessonItemRepository
                .findTop1ByChapterIdAndOrderNumLessThanAndStatusNotOrderByOrderNumDesc(lessonId, itemOrderNum, 1);
        model.last = false;
        if (lastItem != null) {
            model.last = true;
            model.lastItemId = lastItem.getId();
            model.lastItemTitle = lastItem.getTitle();
            model.orderNum = lastItem.getOrderNum();
        }

        // 下一个
        YxyLessonsTopicLessonItem nextItem = yxyLessonsTopicLessonItemRepository
                .findTop1ByChapterIdAndOrderNumGreaterThanAndStatusNotOrderByOrderNumAsc(lessonId, itemOrderNum, -1);
        model.next = false;
        if (nextItem != null) {
            model.next = true;
            model.nextItemId = nextItem.getId();
            model.nextItemTitle = nextItem.getTitle();
            model.orderNum = nextItem.getOrderNum();
        }
        //
        model.title = item.getTitle();
        model.intro = item.getIntro();

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String itemId;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(itemId, null), "章节ID不能为空");
        }
    }

    protected static class RespModel {
        public String preview;
        public String title;
        public String intro;
        public Integer orderNum;
        public String duration;

        // 是否拥有上一个
        public boolean last;
        public String lastItemId;
        public String lastItemTitle;

        // 是否拥有下一个
        public boolean next;
        public String nextItemId;
        public String nextItemTitle;
    }
}
